(function() {
	var app = angular.module('distributeddatabasemonitorApp', [ 'ui.bootstrap' ]);
	app.controller('distributeddatabasemonitorController',
					function($http, $scope, $log, $uibModal, $sce) {
						var controller = this;
						var basePath = $scope.getAppBasePath();
						$scope.serviceInfo = {};
						// 参数定义
						controller.queryCondition = {
							"systemName":"",
							"hostName":"",
							"page" : 1,
							"size" : 4,
						};
						controller.paginationInfo = {
							"maxSize" : 5,
							"totalPages" : 0,
							"totalElements" : 0
						};
						controller.pageChanged = function() {
							loadData();
						}
						
						//单选框
						$scope.onChange = function(data){
							for(var i=0;i<controller.dataList.length;i++){
								if(data.id == controller.dataList[i].id){
									controller.dataList[i].checked = true;
								}else{
									controller.dataList[i].checked = false;
								}
							} 
							
							//事件响应
							controller.sytemParameterId = data.sytemParameterId;
							$scope.url = '';
							$.ajax({
								type : "POST",
								url : basePath
										+ "/get/getGrafanaUrl",
								dataType : "json",
								contentType : "application/json",
								data : JSON
										.stringify(controller.sytemParameterId),
								success : function(data) {
									$scope.url = $sce.trustAsResourceUrl(data);
									$scope.$apply(); 
								},
							error : function(XMLHttpRequest, textStatus){
								$scope.url = $sce.trustAsResourceUrl('www.baidu.com');
								$scope.$apply(); 
							}
							});
						}
						
						//查询事件
						this.search = function(){
							controller.queryCondition.systemName = $scope.controller.queryCondition.systemName;
							controller.queryCondition.hostName = $scope.controller.queryCondition.hostName;
							loadData();
//							$scope.url = '';
//							$scope.$apply();
						}
						// 重置事件
						this.reset = function() {
							$scope.controller.queryCondition.hostName = '';
							$scope.controller.queryCondition.systemName = '';
						}
						// 程序初期化
						this.init = function() {
							loadData();
						}

						// 程序初期化
						this.init();

						
/*以上为原代码以下为修改*/
						
						// 选择oracle弹窗  
						this.selectDDatabaseZTreeHtml = function(data) {
							
							var modalInstanceAdd = $uibModal.open({
										backdrop : 'static',
										keyboard : false,
										scope : $scope,
										templateUrl : 'js/controller/view.display/hostMonitorZTree.html',// 模态框的页面内容,这里的url是可以自己定义的,也就意味着什么都可以写
										controller : 'oracleZTreeController',// 这是模态框的控制器,是用来控制模态框的
										ariaLabelledBy : 'modal-title',
										ariaDescribedBy : 'modal-body',
									});

							modalInstanceAdd.opened.then(function(){//模态窗口打开之后执行的函数  
								//获取树状图数据
								getTreeData("tuopu");
				        
							}); 
						}
						
						// 获取树状图数据
						function getTreeData(paramater) {
							var basePath = $scope.getAppBasePath();
							$.ajax({
								type : "POST",
								url : basePath + "/get/queryViewDisplayZtree",
								dataType : "json",
								data:{
									"monitorCenterHostInfomationType":"1"     
								}, 
							/*	0和999 主机监控
								1     分布式数据库监控
								2     消息中间件
								3     分布式缓存监控
								4 oracle监控      */
								success : function(data) {
									var resultArrary = JSON.parse(data);

									var setting = {
										callback : {
//											beforeAsync: zTreeBeforeAsync,
											onClick : zTreeOnClick,
											onExpand: zTreeOnExpand//点击节点前+号加载
										}
									};
									function zTreeBeforeAsync(treeId, treeNode) {
									    return (treeNode.isParent == true);
									};
									// 返回数据转成json
									function ajaxDataFilter(treeId, parentNode, responseData) {
										responseData = JSON.parse(responseData);
										return responseData;
									}
									;
									var zTreeObj = $.fn.zTree.init($("#tuopu"), setting,
											resultArrary);
									$("#search-bt").click(filter);
									//默认点击展开根目录
									$("#tuopu_1_switch").click();
									function zTreeOnExpand(event, treeId, treeNode){
										var nodes = zTreeObj.getSelectedNodes();
										if (nodes.length>0 && treeNode.level != 0 && treeNode.level != 1) {
											zTreeObj.reAsyncChildNodes(nodes[0], "refresh");
											}
									}
									function zTreeOnClick(event, treeId, treeNode) {
										var te = treeNode.sytemParameterId;
										var serchId=document.getElementById("serchConfirm");
										serchId.setAttribute("value",te);
										if(treeNode.isParent == false){
											$scope.serviceInfo.sytemParameterId  = treeNode.sytemParameterId;
											
											//显示是主机名
											$("#selectDiv").html(treeNode.name);
											//关闭弹窗
											$("#hosttreecancel").click();
										}
									}
									
									//过滤ztree显示数据
									var hiddenNodes;
									function filter(){
										//显示上次搜索后背隐藏的结点
										zTreeObj.showNodes(hiddenNodes);
										var _keywords=$("#keyword").val();
										if(_keywords == null || _keywords ==""){
											zTreeObj.showNodes(hiddenNodes);
										}else{
											var nodes = zTreeObj.getNodesByParamFuzzy("name", _keywords, null);  //根据节点数据的属性搜索，获取条件模糊匹配的节点数据 JSON 对象集合,(keywords)模糊匹配只能针对 String 类型的数据
											if (nodes.length>0) { 
												zTreeObj.selectNode(nodes[0]); //会让节点自动滚到到可视区域内
											} 

											//查找不符合条件的叶子节点
											function filterFunc(node){
												var _keywords=$("#keyword").val();
												if(node.isParent||node.name.indexOf(_keywords)!=-1){
													return false;
												}else{
													return true;	
												}
											};

										}
									};
									//弹窗title显示
								$("#modalTitle").html("选择分布式数据库");

								}
							});
				}
						
						//开始
						this.showResult=function (){
								$scope.url = '';
								var serchId=document.getElementById("serchConfirm");
								$scope.serviceInfo.sytemParameterId  = serchId.getAttribute("value");
								var basePath = $scope.getAppBasePath();
								$.ajax({
									type : "POST",
									url : basePath
											+ "/get/getGrafanaUrl",
									dataType : "json",
									contentType : "application/json",
									data : JSON
											.stringify($scope.serviceInfo.sytemParameterId),
									success : function(data) {
										$scope.url = $sce.trustAsResourceUrl(data);
										$scope.$apply(); 
									},
								error : function(XMLHttpRequest, textStatus){
									$scope.url = $sce.trustAsResourceUrl('about:blank');
									$scope.$apply(); 
								}
								});
								
							}
								
								
						// 数据查询方法
						function loadData() {;
							$.ajax({
										type : "POST",
										url : basePath
												+ "/data/selectDistributedDatabaseMonitorInfo",
										dataType : "json",
										contentType : "application/json",
										 data : JSON
										 .stringify(controller.queryCondition),
										success : function(data) {
											var result = angular.fromJson(data);
											controller.dataList = result.data;
											controller.paginationInfo.totalPages = result.totalPages;
											controller.paginationInfo.totalElements = result.totalElements;
											// 手动刷新
											$scope.$apply();
										}
									});
						}

					})
			.controller(
					'servicePopuController',
					function($scope, $uibModalInstance, $http, $log) {
						$scope.load = function() {
							// 原系统数据前后台交互
							var basePath = $scope.getAppBasePath();
							$scope.serviceInfo.HostSequenceNumber = "";
							$scope.serviceInfo.ip = "";
							$scope.serviceInfo.hostName = "";
							$scope.serviceInfo.useResponsibilityDepartment = "";
							$scope.serviceInfo.useResponsibility = "";
							$scope.serviceInfo.maintenanceResponsibleDepartment = "";
							$scope.serviceInfo.maintenanceResponsiblePerson = "";
							$
									.ajax({
										type : "POST",
										url : basePath + "/get/selectDistributedDatabaseMonitorInfo",
										dataType : "json",
										contentType : "application/json",
										data : JSON.stringify($scope.parmeater),
										success : function(data) {
											var jsonResult = angular
													.fromJson(data);
											$scope.serviceInfo.HostSequenceNumber = jsonResult.HostSequenceNumber;
											$scope.serviceInfo.ip = jsonResult.ip;
											$scope.serviceInfo.hostName = jsonResult.hostName;
											$scope.serviceInfo.useResponsibilityDepartment = jsonResult.useResponsibilityDepartment;
											$scope.serviceInfo.useResponsibility = jsonResult.useResponsibility;
											$scope.serviceInfo.maintenanceResponsibleDepartment = jsonResult.maintenanceResponsibleDepartment;
											$scope.serviceInfo.maintenanceResponsiblePerson = jsonResult.maintenanceResponsiblePerson;
											$scope.$apply();
										}
									});

						};
						$scope.cancel = function() {
							$uibModalInstance.dismiss('cancel');
						};
					})
					//ztree弹窗js
					.controller(
							'oracleZTreeController',
							function($scope, $uibModalInstance, $http, $log,$filter) {
								//关闭事件
								$scope.cancel = function() {
									// dismiss也是在模态框关闭的时候进行调用,而它返回的是一个reason
									$uibModalInstance.dismiss('cancel');
								};
							});
})();